package com.yootk.servlet;

import com.yootk.common.util.ParameterUtil;
import com.yootk.servlet.handler.ImportHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/import.action")
public class ImportServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        ParameterUtil pu = new ParameterUtil(req);
        try {
            List<String> fileNames = pu.getTempFileNames(); // 获取上传文件名称
            System.err.println(req.getServletContext().getRealPath("/temp/"));
            new ImportHandler(req.getServletContext().getRealPath("/temp/") + fileNames.get(0)).saveData();
            resp.getWriter().println("数据导入成功！"); // 简单一些，直接输出
            pu.clean(); // 清空上传临时文件
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().println("数据导入失败！"); // 简单一些，直接输出
        }
    }
}
